package me.send.business.service.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import me.send.business.base.ErrorEntity;
import me.send.business.base.Page;
import me.send.business.base.SendVariable;
import me.send.business.service.BaseService;
import me.send.business.service.mapper.PopuserMapper;
import me.send.business.service.pojo.BrokerInfoBean;
import me.send.framework.util.HelperUtil;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class PopuserService extends BaseService {

	private final static int COLLECT_BROKER = 1;
	private final static int CANCEL_COLLECT_BROKER = 2;
	
	@Autowired
	private PopuserMapper popuserMapper;

	public Map<String, Object> page_userid(Page page, Integer userid,
			String devicenumber) {
		Map<String, Object> map = new HashMap<String, Object>();
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		// 返回数据和错误码
		ErrorEntity entity = error_1001();
		if (isDevicenumber(devicenumber)) {
			if (page == null) {
				page = new Page();
			}
			Map<String, Object> parameter = new HashMap<String, Object>();
			parameter.put("page", page);
			parameter.put("userid", userid);

			list = popuserMapper.page_userid(parameter);

			entity = error_1000();
		} else {
			entity = error_1002();
		}
		map.put(SendVariable.COMM_ERROR, entity);
		map.put(SendVariable.COMMON_LIST, list);
		return map;
	}

	/**
	 * 用户收藏经纪人
	 * 
	 * @param userid
	 * @param brokerid
	 * @return
	 */
	public int collectionBroker(int userid, int brokerid) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("userid", userid);
		params.put("brokerid", brokerid);
		if (popuserMapper.isCollectedBroker(params)>0) {
			popuserMapper.cancelCollectBroker(params);
			return CANCEL_COLLECT_BROKER;
		}
		params.put("createtime", HelperUtil.getCurrentDate());
		params.put("updatetime", HelperUtil.getCurrentDate());
		popuserMapper.collect_broker(params);
		return COLLECT_BROKER;
	}

	/**
	 * 获取经纪人信息详情
	 * 
	 * @param brokerid
	 * @return
	 */
	public BrokerInfoBean getBrokerInfo(int brokerid,int userid) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("brokerid", brokerid);
		params.put("userid", userid);
		return popuserMapper.getBrokerInfo(params);
	}

	/**
	 * 获取收藏的经纪人集合
	 * @param userid
	 * @return
	 * @author maxmin
	 * @date 2015年9月2日上午9:31:52
	 */
	public List<Map<String, Object>> collectionBrokerList(int userid,int startid,int size) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("userid", userid);
		params.put("startid", startid);
		params.put("size", size);
		return popuserMapper.collectionBrokerList(params);
	}
}
